package com.vmladenov.cook.core;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.widget.ImageView;

import com.vmladenov.cook.CustomApplication;
import com.vmladenov.cook.core.db.DataHelper;
import com.vmladenov.cook.core.objects.User;

public final class Helpers
{
	public static synchronized DataHelper getDataHelper()
	{
		return CustomApplication.getInstance().getDataHelper();
	}

	public static synchronized User getCurrentUser()
	{
		return CustomApplication.getInstance().getCurrentUser();
	}
	
	public static synchronized Context getApplicationContext()
	{
		return CustomApplication.getInstance().getApplicationContext();
	}
	

	public static synchronized boolean isLoggedIn()
	{
		return getCurrentUser().getId() != -1;
	}

	public static boolean isOnline(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) (context.getSystemService(Context.CONNECTIVITY_SERVICE));
		NetworkInfo info = cm.getActiveNetworkInfo();
		AppSettings settings = new AppSettings(context);
		if (settings.useOnlyWiFi() && info.getType() != ConnectivityManager.TYPE_WIFI)
			return false;
		return info != null && info.isConnected() && !info.isRoaming();
	}

	public static Bitmap getImage(String url, Context context)
	{
		try
		{
			AppSettings settings = new AppSettings();
			if (settings.downloadImages() && isOnline(context))
			{
				URL imageUrl = new URL(url);
				HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
				conn.setDoInput(true);
				conn.connect();
				InputStream is = conn.getInputStream();

				return BitmapFactory.decodeStream(is);
			}
			return null;
		}
		catch (IOException e)
		{
		}
		return null;
	}

	public static void setImageFromUrlAsync(ImageView image, final String url)
	{
		if (url == null || url == "") return;
		final Handler handler = new Handler();
		new Thread(new ParameterizedRunnable<ImageView>(image)
		{
			@Override
			public void ParameterizedRun(ImageView imageParam)
			{
				final Bitmap bitmap = Helpers.getImage(url, imageParam.getContext());
				handler.post(new ParameterizedRunnable<ImageView>(imageParam)
				{
					@Override
					public void ParameterizedRun(ImageView imageParam)
					{
						imageParam.setImageBitmap(bitmap);
					}
				});
			}
		}).start();
	}

	public static void copyFile(InputStream in, OutputStream out) throws IOException
	{
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1)
		{
			out.write(buffer, 0, read);
		}
	}

	public static void copyDatabase(Context context, OutputStream out) throws IOException
	{
		AssetManager am = context.getAssets();
		byte[] b = new byte[1024];
		int i, r;
		String format = "cook%d.db";
		for (i = 1; i < 7; i++)
		{
			String fn = String.format(format, i);
			InputStream is = am.open(fn);
			while ((r = is.read(b)) != -1)
				out.write(b, 0, r);
			is.close();
		}
		out.close();
	}

	public static void saveFile(Bitmap bitmap, String path)
	{
		if (bitmap == null || bitmap.getHeight() < 1) return;
		try
		{
			OutputStream outputStream = new FileOutputStream(path);
			BufferedOutputStream bos = new BufferedOutputStream(outputStream);
			bitmap.compress(CompressFormat.JPEG, 90, bos);
			bos.flush();
			bos.close();
			outputStream.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
